﻿using HealthcareSystem.API.Read.Application.Command.Disease;
using HealthcareSystem.API.Read.Application.Command.Element.ElementCommand;
using HealthcareSystem.API.Read.DTO.Disease;
using HealthcareSystem.API.Read.DTO.Element;
using HealthcareSystem.Domain.BasicSettings.Diseasemanagement;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Read.Application.CommandHandel.Disease
{
    public class ShowDiseaseCommandHandel : IRequestHandler<ShowDiseaseCommand, ApiResult<ApiPaging<List<ShowDiseasemanageDto>>>>
    {
        IBaseRepository<Diseasemanage> diseasemanageHandel;
        IBaseRepository<Listofdiseases> listofdiseasesHandel;

        public ShowDiseaseCommandHandel(IBaseRepository<Diseasemanage> diseasemanageHandel, IBaseRepository<Listofdiseases> listofdiseasesHandel)
        {
            this.diseasemanageHandel = diseasemanageHandel;
            this.listofdiseasesHandel = listofdiseasesHandel;
        }
        /// <summary>
        /// 病种显示列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<ApiPaging<List<ShowDiseasemanageDto>>>> Handle(ShowDiseaseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<List<ShowDiseasemanageDto>>> result = new ApiResult<ApiPaging<List<ShowDiseasemanageDto>>>();
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();

            var list = from a in diseasemanageHandel.GetAll()
                       from b in listofdiseasesHandel.GetAll()
                       where b.ListofdiseasesId == a.ListofdiseasesId
                       select new ShowDiseasemanageDto
                       {
                           CodeId = a.CodeId,
                           ListofdiseasesId = b.ListofdiseasesId,
                           CodeName = a.CodeName,
                           CodeNum = a.CodeNum,
                           Colormarking = a.Colormarking,
                           ListofdiseasesName = b.ListofdiseasesName,


                       };

            // 2. 按元素类别（分类）过滤
            if (request.ListofdiseasesId.HasValue && request.ListofdiseasesId.Value > 0)
            {
                list = list.Where(x => x.ListofdiseasesId == request.ListofdiseasesId.Value);
            }


            var totalCount = list.Count();
            var totalPage = (int)Math.Ceiling((totalCount * 1.0) / request.pageSize);
            list = list.OrderBy(x => x.CodeId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize);


            ApiPaging<List<ShowDiseasemanageDto>> paging = new ApiPaging<List<ShowDiseasemanageDto>>();
            paging.totalPage = totalPage;
            paging.totalCount = totalCount;
            paging.Data = list.ToList();
            result.Data = paging;

            return Task.FromResult(result);
        }

      



    }
}
